<template>
<div>

  <div style="padding: 10px 0">
    <el-input placeholder="请输入垃圾名" style="width: 200px;" suffix-icon="el-icon-search" v-model="name"></el-input>
    <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
    <el-button  type="warning" @click="reset">重置</el-button>
    <el-button type="primary" @click="handleAdd">新增</el-button>
    <el-popconfirm
        title="您确定批量删除吗？"
        class="ml-5"
        @confirm="delBatch"
    >
      <el-button type="danger"  slot="reference">批量删除<i class="el-icon-remove-outline"></i> </el-button>
    </el-popconfirm>
  </div>
  <el-table :data="tableData" border stripe     @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55">
    </el-table-column>
    <el-table-column prop="goodsId" label="ID" >
    </el-table-column>
    <el-table-column prop="name" label="垃圾名" width="140">
    </el-table-column>
    <el-table-column prop="price" label="回收价格">
    </el-table-column>
    <el-table-column  label="图片">
      <template slot-scope="scope">
        <el-image style="width: 30px; height: 30px" :src="scope.row.image" :preview-src-list="[scope.row.image]">
        <div slot="error" class="image-slot">
          <i class="el-icon-picture-outline"></i>
        </div>
      </el-image>
      </template>
    </el-table-column>
    <el-table-column label="操作" >
      <template slot-scope="scope">
        <el-button type="primary" @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i> </el-button>
        <el-popconfirm
            title="您确定删除吗？"
            class="ml-5"
            @confirm="del(scope.row.goodsId)"
        >
          <el-button type="danger"  slot="reference">删除<i class="el-icon-remove-outline"></i> </el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <div style="padding: 10px 0">

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>
  <el-dialog title="垃圾信息" :visible.sync="dialogFormVisible" width="30%">
    <el-form label-width="80px" size="small">
      <el-form-item label="垃圾名" >
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="回收价格" >
        <el-input v-model="form.price" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="图片" >
<!--        图片上传-->
        <el-upload
            class="upload-demo"
            action="http://162.14.65.41:8080/upload"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            :file-list="fileList"
            list-type="picture">
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
  <el-dialog title="垃圾信息" :visible.sync="dialogFormVisible1" width="30%">
    <el-form label-width="80px" size="small">
      <el-form-item label="垃圾名" >
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="回收价格" >
        <el-input v-model="form.price" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="图片">
<!--        修改图片-->
      <el-upload
          class="avatar-uploader"
          action="http://162.14.65.41:8080/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess">
        <img v-if="form.image" :src="form.image" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible1 = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "goods",
  data(){

    return {
      tableData:[],
      fileList:[],
      total:0,
      pageNum:1,
      pageSize:10,
      name:"",
      price:"",
      image:'',
      dialogFormVisible:false,
      dialogFormVisible1:false,
      form:{},
      multipleSelection:[],
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false

    }
  },
  created() {
    this.load()
  },
  methods:{
    load(){
      this.request.get("/ljhsgoods/page",{
        params:{
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          name:this.name
        }
      }).then(res=>{
        console.log(res)
        this.tableData=res.data.records
        this.total=res.data.total
      })
    },
    handleSelectionChange(val){
      this.multipleSelection=val
    },
    handleAdd(){
      this.dialogFormVisible=true
      this.form={}
      console.log(this.form)
    },
    handleSuccess(fileList){
      console.log(fileList)
      let a="http://162.14.65.41:8089/"+fileList
      console.log(a)
      this.form.image=a
    },
    save(){
      this.form.num=1;
      this.request.post("/ljhsgoods",this.form).then(res=>{
        if(res){
          this.$message.success("保存成功")
          this.dialogFormVisible=false
          this.load()
        }else{
          this.$message.error("保存失败")
        }
      })
    },
    reset(){
      this.name=""
      this.load()
    },
    del(goodsId){
      this.request.delete("/ljhsgoods/"+goodsId).then(res=>{
        if (res) {
          this.$message.success("删除成功")
          this.load()
        }else {
          this.$message.error("删除失败")
        }
      })
    },
    delBatch(){
      let ids=this.multipleSelection.map(v=>v.goodsId)
      this.request.post("/ljhsgoods/batch/del",ids).then(res=>{
        if (res) {
          this.$message.success("批量删除成功")
          this.load()
        }else {
          this.$message.error("批量删除失败")
        }
      })
    },
    handleEdit(row){
      this.form=row
      this.dialogFormVisible1=true
    },
    handleSizeChange(pageSize){
      this.pageSize=pageSize
      this.load()
    },
    handleCurrentChange(pageNum){
      this.pageNum=pageNum
      this.load()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },

    handleAvatarSuccess(res){
      console.log(res)
      this.form.image="http://162.14.65.41:8089/"+res
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>